<?php
declare (strict_types=1);

namespace app\api\controller;

use app\api\model\Collage;
use app\api\model\CollageGoods;
use app\api\model\CollageMember;
use app\api\model\Goods;
use app\api\model\GroupMaster;
use app\api\model\SpecGoods;
use app\serve\controller\Token;
use think\Request;

class GroupBooking
{
    /**
     * @return string|\think\response\Json
     * 拼团活动列表
     */
    public function GroupBookingGoods()
    {
        $collageGoodsModel = new CollageGoods();
        $collageGoodsData = $collageGoodsModel->collageGoodsList();
        if (!$collageGoodsData) {
            return json(['code' => 10002, 'msg' => '暂无拼团商品', 'data' => []]);
        }
        $data = [];
        foreach ($collageGoodsData as $k => $v) {
            $res = [
                'id' => $v['id'],
                'goods_sku_id' => $v['goods_sku_id'],
                'collage_name' => $v['collage_name'],
                'goods_name' => $v['SpecGoods']['goods']['goods_name'],
                'goods_banner' => $v['SpecGoods']['goods']['goods_banner'],
                'goods_price' => $v['SpecGoods']['goods']['goods_price'],
                'goods_collage_price' => $v['SpecGoods']['goods']['collage_price'],
                'collage_people_num' => $v['collage_people_num']
            ];
            array_push($data, $res);
        }
        return json($data);
    }

    /**
     * @param Request $request
     * @return string|\think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * 用户参加的拼团活动
     */
    public function userCollage(Request $request)
    {
        $memberInfo = CollageMember::where('user_id',1)->with('collage.collageGoods')->select();
        $data = [];
        foreach ($memberInfo as $k => $v)
        {
            $goods_id = SpecGoods::find($v['collage']['collageGoods']['goods_sku_id'])->value('goods_id');
            $goods_banner = Goods::find($goods_id)->value('goods_banner');
            $group_master_name = GroupMaster::find($v['collage']['master_id'])->value('true_name');
            $yet_collage_num = CollageMember::where('collage_id',$v['collage_id'])->count();
            $surplus_collage_num = $v['collage']['collageGoods']['collage_people_num'] - $yet_collage_num;
            $info = [
                'collage_goods_id' => $v['collage']['collageGoods']['id'],
                'goods_banner' => $goods_banner,
                'group_master_name' => $group_master_name,
                'yet_collage_num' => $yet_collage_num,
                'surplus_collage_num' => $surplus_collage_num
            ];
            array_push($data,$info);
        }
        return json($data);
    }

    /**
     * @param Request $request
     * @return string|\think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * 用户参加拼团活动
     */
    public function joinCollage(Request $request)
    {
        //第一步先取token
        $token = $request->header('token');
        //jwt进行校验token
        $res = (new Token())->chekToken($token);
        if ($res['code'] != 1) {
            return json(['error_code' => 10002, 'msg' => $res['msg'], 'data' => ''], 400);
        }
        //从token中获取用户id
        $user_id = $res['data']->user_id;
        $collage_goods_id = $request->param('collage_goods_id');
        $collageModel = new Collage();
        $collageInfo = $collageModel->collageInfo($collage_goods_id);
        if (!$collageInfo){
            return json(['code'=>10003,'msg'=>'没有此拼团信息,请刷新页面后重试','data'=>[]]);
        }
        $collage_id = $collageInfo->id;
        $collageMemberModel = new CollageMember();
        $where = ['collage_id'=>$collage_id,'user_id'=>$user_id];
        $collageMemberInfo = $collageMemberModel->collageMemberInfo($where);
        if ($collageMemberInfo){
            return json(['code'=>10003,'msg'=>'您已参加过此拼团','data'=>[]]);
        }
        $memberNum = $collageMemberModel->memberNum($collage_id);
        $collageGoodsModel = new CollageGoods();
        $collage_people_num = $collageGoodsModel->collageGoodsInfo($collage_goods_id)->collage_people_num;
        $collage_end_time = $collageGoodsModel->collageGoodsInfo($collage_goods_id)->collage_end_time;
        $surplus_people_num = $collage_people_num-$memberNum-1;
        if ($collage_end_time <= date('Y-m-d H:i:s',time())){
            return json(['code'=>10002,'msg'=>'拼团已结束','data'=>[]]);
        }
        if ($memberNum >= $collage_people_num){
            return json(['code'=>10002,'msg'=>'该团人数已满,拼团失败','data'=>[]]);
        }
        $joinData = ['collage_id'=>$collage_id,'user_id'=>$user_id,'surplus_people_num'=>$surplus_people_num];
        $joinRes = $collageMemberModel->joinMember($joinData);
        if (!$joinRes){
            return json(['code'=>10002,'msg'=>'参加拼团失败','data'=>[]]);
        }
        return json(['code'=>10001,'msg'=>'拼团成功','data'=>[]]);
    }

    /**
     * @param Request $request
     * @return string|\think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * 参加此拼团的用户列表
     */
    public function collageRecord(Request $request)
    {
        $collage_goods_id = $request->param('collage_goods_id');
        $collageModel = new Collage();
        $collageInfo = $collageModel->collageInfo($collage_goods_id);
        if (!$collageInfo){
            return json(['code'=>10003,'msg'=>'没有此拼团信息,请刷新页面后重试','data'=>[]]);
        }
        $collage_id = $collageInfo->id;
        $collageMemberModel = new CollageMember();
        $collageList = $collageMemberModel->collageList($collage_id);
        $collageGoodsModel = new CollageGoods();
        $collage_people_num = $collageGoodsModel->collageGoodsInfo($collage_goods_id)->collage_people_num;
        $groupMasterInfo = GroupMaster::with('user')->find($collageInfo->master_id);
        $groupMasterData = [
            'nickname' => $groupMasterInfo['nickname'],
            'avator' => $groupMasterInfo['avator'],
            'collage_people_num' => $collage_people_num
        ];
        array_unshift($collageList,$groupMasterData);
        return json($collageList);
    }
}
